import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)

export default {
  getRoles:function (cb) {
    var currentUser=AV.User.current() //获取当前用户
    if(!currentUser) return cb(false);
    currentUser.getRoles().then(function (roles) {   //获取当前用户的权限 不同的权限功能不同
      var isAdmin=true;
      for(var i=0;i<roles.length;i++) {
        if(roles[i].toJSON().name==='teacher'){
          isAdmin=false;
          break
        }
        if(!isAdmin) return cb(false);
        else return cb(true)
      }
    })
  },
  getAllData:function (cb) {
    var self = this
    this.getRoles(function (result) {
      var subjects =[]
      if (result === true){
        self.adminSubLesson(subjects,function (result) {
          cb(result)
        })
      }else {
        self.teacherSubLesson(subjects,function (result) {
          cb(result)
        })
      }
    })
  },
  //获取admin的全部科目课程
  adminSubLesson:function(subjects,cb){
    var self = this
    var priorityQuery = new AV.Query('Lesson');
    priorityQuery.equalTo('isPublished', true);
    var statusQuery = new AV.Query('Lesson');
    statusQuery.equalTo('isChecked', 1);
    var query = AV.Query.or(priorityQuery, statusQuery);
    query.find().then(function (result) {
      result.forEach(function (key) {
        var checkedStatus = key.attributes.isChecked
          subjects.push({'name': key.attributes.name, 'id': key.id, 'isChecked': checkedStatus,'tags':key.attributes.tags})
          if(checkedStatus ==1){
            self.getHistoryLesson(key.id,function (result) {
              Vue.http.get(result).then(function (response) {
                subjects.push({'name':response.data.name,'id':response.data.id ,'verstion':'his','tags':response.data.tags})
              })
            })
          }
      })
      cb(subjects)
    },function (err) {
      console.log(err)
    })
  },
  //获取老师的全部科目课程
  //获取老师的全部课程
  teacherSubLesson:function(subjects,cb){
    var self = this
    var query = new AV.Query('Lesson')
    query.equalTo('isPublished',true)
    query.find().then(function (result) {
      result.forEach(function (key) {
        if(key.attributes.isChecked ===3 || key.attributes.isChecked ===0){
          subjects.push({'name':key.attributes.name,'id':key.id })
        }else if (key.attributes.isChecked === 1 || key.attributes.isChecked === 2) {
          var lessonId = key.id
          self.getHistoryLesson(lessonId,function (result) {
            Vue.http.get(result).then(function (response) {
              subjects.push({'name':response.data.name,'id':response.data.id ,'verstion':'his'})
            },function (err) {

            })
          })
        }
      })
      cb(subjects)
    },function (err) {
      console.log(err)
    })
  },
  getHistoryLesson:function (lessonId,cb) {
    console.log(lessonId)
    var startDateQuery = new AV.Query('LessonSnapshot');
    startDateQuery.equalTo('lessonId',lessonId)
    var endDateQuery = new AV.Query('LessonSnapshot');
    startDateQuery.equalTo('isChecked', 3);
    var query = AV.Query.and(startDateQuery, endDateQuery);
    query.find().then(function (result) {
      if (result.length == 1){
        cb(result[0].toJSON().manifest_json.url)
      }else if (result.length >1){
        var version = 1
        var lesson =''
        result.forEach(function (item) {
          if (item.attributes.version_code > version){
            version = item.attributes.version_code
            lesson = item.toJSON().manifest_json.url
          }
        })
        cb(lesson)
      }

    })
  },
  checkoutLike:function (cb) {
    var self = this
    var currentUser = AV.User.current()
    var userId = currentUser.id
    var lessonIds=[]
    var startDateQuery = new AV.Query('Favourite')
    var todoFolder = AV.Object.createWithoutData('User', userId);
    startDateQuery.equalTo('user', todoFolder)
    var endDateQuery = new AV.Query('Favourite');
    endDateQuery.equalTo('action', true)
    var query = AV.Query.and(startDateQuery, endDateQuery);
    query.find().then(function (result) {
      result.forEach(function (item) {
        lessonIds.push(item.toJSON().lessonId)
      })
      cb(lessonIds)
    })
  }

}
